import 'package:flutter/material.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/handle/xindong_user_handle.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_common_widget.dart';
import 'package:xindong/widgets/container/xindong_empty_container.dart';
import 'package:xindong/widgets/xindong_avatar.dart';

class XinDongBlacklistYeMian extends StatefulWidget {
  const XinDongBlacklistYeMian({Key? key}) : super(key: key);

  @override
  State<XinDongBlacklistYeMian> createState() => _XinDongBlacklistYeMianState();
}

class _XinDongBlacklistYeMianState extends State<XinDongBlacklistYeMian> {
  final RefreshController _refreshController = RefreshController(initialRefresh: true);
  List? dataList;

  void _onRefresh() async {
    final object = await XinDongNetworking.instance.request(XinDongApi.blackList, method: XinDongHttpMethod.post);
    if (object.code == XinDongServerStatus.success) {
      setState(() {
        dataList = object.result;
      });
    }
    _refreshController.refreshCompleted();
  }

  Future<void> _onDelete(dynamic userId) async {
    final result = await XinDongUserHandle().blackListAdd(userId: userId);
    if (result) {
      dataList!.removeWhere((element) => element["user_id"] == userId);
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("黑名单"),
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: false,
        controller: _refreshController,
        onRefresh: _onRefresh,
        child: XinDongEmptyContainer(
          dataList: dataList,
          child: ListView.separated(
            itemCount: dataList?.length ?? 0,
            itemBuilder: (BuildContext context, int index) {
              Map data = dataList![index];
              return Container(
                height: 50,
                color: DZColors.cellColor,
                padding: const EdgeInsets.only(left: kPaddingValue, right: 5),
                child: Row(
                  children: [
                    XinDongAvatar(url: data['user_head'], size: 40),
                    const SizedBox(width: 10),
                    Expanded(
                      child: Text(
                        data['user_name'],
                        style: const TextStyle(fontSize: 15, color: DZColors.titleColor),
                      ),
                    ),
                    TextButton(
                      onPressed: () => _onDelete(data["user_id"]),
                      child: const Text(
                        "移出黑名单",
                        textAlign: TextAlign.end,
                        style: TextStyle(fontSize: 12, color: Colors.red),
                      ),
                    ),
                  ],
                ),
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return commonDivider();
            },
          ),
        ),
      ),
    );
  }
}
